package com.xyoto.community.controller;

import com.xyoto.community.aspect.annotation.RequiresLogin;
import com.xyoto.community.dto.*;
import com.xyoto.community.service.TopicService;
import com.xyoto.community.utils.PageUtils;
import com.xyoto.community.utils.Result;
import com.xyoto.community.vo.TopicDetailVo;
import com.xyoto.community.vo.TopicListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/topic")
@Api(value = "话题（前台）", tags = "话题（前台）")
public class TopicController {

    @Autowired
    private TopicService topicService;

    @ApiOperation("获取话题列表")
    @GetMapping("/getTopicList")
    public Result<PageUtils<TopicListVo>> getTopicList(PageDTO pageDTO){
        return topicService.getTopicList(pageDTO);
    }

    /**
     * 关注话题/取关话题
     * @param saveTopicFollowDTO
     * @return
     */
    @RequiresLogin
    @ApiOperation("关注/取关话题")
    @PostMapping("/saveFollow")
    public Result saveTopicFollow(@RequestBody @Valid SaveTopicFollowDTO saveTopicFollowDTO){
        return topicService.saveTopicFollow(saveTopicFollowDTO);
    }

    @ApiOperation("获取话题详情")
    @ApiImplicitParam(name = "topicId" , value = "话题id",required = true)
    @GetMapping("/getTopic")
    public Result<TopicDetailVo> getTopicDetail(Integer topicId){
        return topicService.getTopicDetail(topicId);
    }

    /**
     * 新增话题功能
     * @param saveTopicDto
     * @param path
     * @return 已弃用
     * @throws IOException
     */
//    @ApiOperation(value = "新增话题（前台）", notes = "新增话题（前台）")
//    @PostMapping("/saveTopic")
    public Result saveTopic(SaveTopicDTO saveTopicDto,
                            MultipartFile path) throws IOException {
        return topicService.saveTopic(saveTopicDto,path);
    }

    /**
     * 获取热门话题榜
     * @param hotTopicDTO
     * @return
     */
    @ApiOperation("获取热门话题榜")
    @GetMapping("/getHotTopicList")
    public Result getHotTopicList(@Valid GetHotTopicDTO hotTopicDTO){
        return topicService.getHotTopicList(hotTopicDTO);
    }

    /**
     * 获取话题热门动态
     * @param getTopicHotDynamicDTO
     * @return
     */
    @ApiOperation("获取话题热门动态")
    @GetMapping("/getTopicHotDynamic")
    public Result getTopicHotDynamic(@Validated GetTopicHotDynamicDTO getTopicHotDynamicDTO){
        return topicService.getTopicHotDynamic(getTopicHotDynamicDTO,getTopicHotDynamicDTO.getTopicId());
    }

    @ApiOperation("获取话题全部动态")
    @GetMapping("/getTopicAllDynamic")
    public Result getTopicAllDynamic(@Validated GetTopicAllDynamicDTO getTopicAllDynamicDTO){
        return topicService.getTopicAllDynamic(getTopicAllDynamicDTO,getTopicAllDynamicDTO.getTopicId());
    }

    /**
     * 话题审核
     * @return
     */
    @RequiresLogin
    @ApiOperation("发布话题")
    @PostMapping("/saveTopicExamine")
    public Result topicExamine(@Validated SaveTopicDTO dto){
        return topicService.saveTopicExamine(dto);
    }

    @ApiOperation("获取话题用户榜单列表")
    @GetMapping("/getTopicUserHotList")
    public Result getTopicUserHotList(@RequestParam("topicId") Integer topicId){
        return topicService.getTopicUserHotList(topicId);
    }

}
